package com.supersoul.myself.controller.user;

import com.supersoul.myself.entity.User;
import com.supersoul.myself.entity.vo.LoginVO;
import com.supersoul.myself.enums.ServiceEnum;
import com.supersoul.myself.ex.ServiceException;
import com.supersoul.myself.service.UserService;
import com.supersoul.myself.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R login(@RequestBody @Valid LoginVO loginVO) throws ServiceException {
        User user = userService.login(loginVO);
        if (user == null) {
            return R.failed(ServiceEnum.VALID_CODE_EROOR);
        }
        return R.success(user);
    }


    @GetMapping("/codeToPhone")
    public R codeToPhone(@RequestParam String phone) throws Exception {
        userService.codeToPhone(phone);
        return R.success();
    }



}
